package DTO;

import global.MyArrayList;

import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import database.DatabaseHelper;

public class QuestionDTO {

	//Properties
	private String ID;
	private String Content;
	private String ImagePath;
	
	private ArrayList<AnswerDTO> Answers;
	private ArrayList<CorrectAnswerDTO> CorrectAnswers;
	
	//Property ID
	public String getID() { return ID; }
	public void setID(String id) { this.ID = id; }
	
	//Property Content
	public String getContent() { return Content; }
	public void setContent(String content) { this.Content = content; }
	
	//Property ImagePath
	public String getImagePath() {
		return ImagePath;
	}
	public void setImagePath(String imagePath) {
		ImagePath = imagePath;
	}
	
	
	//Property Answers
	public ArrayList<AnswerDTO> getAnswers() { return Answers; }
	public void setAnswers(ArrayList<AnswerDTO> answers) { this.Answers = answers; }

	//Property CorrectAnswers
	public ArrayList<CorrectAnswerDTO> getCorrectAnswers() { return CorrectAnswers; }
	public void setCorrectAnswers(ArrayList<CorrectAnswerDTO> correctAnswers) { this.CorrectAnswers = correctAnswers; }
	
	static int[] ArrayA1A2 = {1,3,4,5,7,8,9,12,13,14,16,17,19,22,24,25,26,27,28,29,30,31,32,35,36,38,39,45,46,47,48,50,51,53,54,55,56,57,58,59,60,61,65,67,68,70,71,80,83,84,85,86,87,88,89,91,92,97,98,99,100,101,102,103,105,112,113,117,126,127,130,131,133,134,136,186,188,193,194,197,256,257,258,262,263,264,265,266,268,269,270,271,274,275,276,277,278,279,280,281,282,283,284,285,289,290,291,298,312,324,325,345,347,349,351,356,359,361,362,364,368,369,372,373,374,375,376,377,378,379,386,389,394,396,397,398,407,408,409,412,417,422,425,428,429,430,431,434,436,437};
	
	
	//Methods
	
	private static String createID(int number)
	{
		String result = "QS";
		if(number<10)
		{
			result+="000"+number;
		}
		else if(number<100)
		{
			result+="00"+number;
		}else if(number<1000)
		{
			result+="0"+number;
		}else
		{
			result+=number;
		}
		return result;
	}
	
	//Method get question
	public static MyArrayList<QuestionDTO> getQuestion(Context context, int curQuestionNo, int questionNo){
		
		MyArrayList<QuestionDTO> questions = new MyArrayList<QuestionDTO>();
		
		//Create object DatabaseHelper
		DatabaseHelper myDbHelper = DatabaseHelper.getInstance(context);
 
	 	try {
	 		myDbHelper.openDatabase();
	 	}catch(SQLException sqle){
	 		throw sqle;
	 	}
	 	
	 	//Condition for select
	 	String condition = "'" + createID(questionNo) + "' and ";
	 	String condition1 = "'" + createID(curQuestionNo) + "'";
	 	
	 	String query = "select * from Questions where ID <= " + condition + "ID >= " + condition1;
	 	
	 	Cursor cursor = myDbHelper.getMyDataBase().rawQuery(query, null);
	 	if(cursor.moveToFirst()){
	 		do{
	 			QuestionDTO question = new QuestionDTO();
	 			
	 			question.setID(cursor.getString(cursor.getColumnIndex("ID")));
	 			//question.setTypeID(cursor.getString(cursor.getColumnIndex("TypeID")));
	 			question.setContent(cursor.getString(cursor.getColumnIndex("Content")));
	 			try{
	 				question.setImagePath(cursor.getString(cursor.getColumnIndex("ImagePath")));
	 			}catch(Exception e){
	 				question.setImagePath("");
	 			}
	 			
	 			ArrayList<AnswerDTO> arrAnswer = AnswerDTO.getAnswers(context, question.getID());
	 			question.setAnswers(arrAnswer);
	 			
	 			ArrayList<CorrectAnswerDTO> arrCorrectAnswer = CorrectAnswerDTO.getCorrectAnswers(context, question.getID());
	 			question.setCorrectAnswers(arrCorrectAnswer);
	 			
	 			questions.add(question, curQuestionNo);	
	 			curQuestionNo++;
	 		}while(cursor.moveToNext());
	 	}
	 	myDbHelper.close();
	 	return questions;
	}

	//Get questions by array ID
	public static ArrayList<QuestionDTO> GetQuestionByListID(Context context, ArrayList<String> arrQuestionIDs)
	{
		if(arrQuestionIDs == null || arrQuestionIDs.isEmpty())
		{
			return new ArrayList<QuestionDTO>();
		}
		else
		{
			String listQuestion = "";
			for(int i = 0; i<arrQuestionIDs.size(); i++)
			{
				if(i < arrQuestionIDs.size() - 1) {
					listQuestion += "'"+arrQuestionIDs.get(i)+"', ";
				}else {
					listQuestion += "'"+arrQuestionIDs.get(i)+"'";
				}
			}
			listQuestion.substring(0, listQuestion.length()-2);
			String query = "SELECT * FROM Questions WHERE ID in ("+listQuestion+")";
			
			ArrayList<QuestionDTO> arrResults = new ArrayList<QuestionDTO>();
			DatabaseHelper myDbHelper = DatabaseHelper.getInstance(context);
		 	try {
		 		myDbHelper.openDatabase();
		 	}catch(SQLException sqle){
		 		throw sqle;
		 	}
		 	
		 	Cursor cursor = myDbHelper.getMyDataBase().rawQuery(query, null);
			if(cursor.moveToFirst()){
				do{
					QuestionDTO resultItem = new QuestionDTO();
	
					//Set properties
					resultItem.setID(cursor.getString(cursor.getColumnIndex("ID")));
					//resultItem.setTypeID(cursor.getString(cursor.getColumnIndex("TypeID")));
					resultItem.setContent(cursor.getString(cursor.getColumnIndex("Content")));
		 			try{
		 				resultItem.setImagePath(cursor.getString(cursor.getColumnIndex("ImagePath")));
		 			}catch(Exception e){
		 				resultItem.setImagePath("");
		 			}
		 			
		 			ArrayList<AnswerDTO> arrAnswer = AnswerDTO.getAnswers(context, resultItem.getID());
		 			resultItem.setAnswers(arrAnswer);
		 			
		 			ArrayList<CorrectAnswerDTO> arrCorrectAnswer = CorrectAnswerDTO.getCorrectAnswers(context, resultItem.getID());
		 			resultItem.setCorrectAnswers(arrCorrectAnswer);
		 			
		 			//Property Answers
		 			//ArrayList<AnswerDTO> arrAnswer = AnswerDTO.getAnswers(context, resultItem.getID());
		 			resultItem.setAnswers(AnswerDTO.getAnswers(context, resultItem.getID()));
		 			
					arrResults.add(resultItem);
				}while(cursor.moveToNext());
			}
		 	myDbHelper.close();
		 	return arrResults;		
		}
	}
	
	
	
	
	//Get questions by array ID
		public static MyArrayList<QuestionDTO> GetQuestionByLicenseType(Context context, String licenseType)
		{
			/*
			if(arrQuestionIDs == null || arrQuestionIDs.isEmpty())
			{
				return new ArrayList<QuestionDTO>();
			}
			else
			{*/
			/*
				String listQuestion = "";
				for(int i = 0; i<arrQuestionIDs.size(); i++)
				{
					if(i < arrQuestionIDs.size() - 1) {
						listQuestion += "'"+arrQuestionIDs.get(i)+"', ";
					}else {
						listQuestion += "'"+arrQuestionIDs.get(i)+"'";
					}
				}
				listQuestion.substring(0, listQuestion.length()-2);
				*/
			String query = "";
			if(licenseType == "B2" || licenseType == "C" || licenseType == "D" || licenseType == "E" || licenseType == "F")
			{
				query = "SELECT * FROM Questions";// WHERE ID in ("+listQuestion+")";
			}
			if(licenseType == "B1")
			{
				query = "select * from Questions Where TypeID<> 'QT0002'";
			}
			if(licenseType == "A3" || licenseType == "A4")
			{
				query = "select * from Questions Where TypeID not in ('QT0004', 'QT0005') and ID not in ('QS0119', 'QS0120', 'QS0147', 'QS0152', 'QS0166')";
			}
			if(licenseType == "A1"|| licenseType == "A2")
			{
				
				String listQuestion = "";
				for(int i = 0; i<ArrayA1A2.length; i++)
				{
					if(i < ArrayA1A2.length - 1) {
						listQuestion += "'"+createID(ArrayA1A2[i])+"', ";
					}else {
						listQuestion += "'"+createID(ArrayA1A2[i])+"'";
					}
				}
				query = "SELECT * FROM Questions WHERE ID in ("+listQuestion+")";
				//listQuestion.substring(0, listQuestion.length()-2);
			}
				
			MyArrayList<QuestionDTO> arrResults = new MyArrayList<QuestionDTO>();
			DatabaseHelper myDbHelper = DatabaseHelper.getInstance(context);
		 	try {
		 		myDbHelper.openDatabase();
		 	}catch(SQLException sqle){
		 		throw sqle;
		 	}
		 	
		 	Cursor cursor = myDbHelper.getMyDataBase().rawQuery(query, null);
		 	int i = 0;
			if(cursor.moveToFirst()){
				do{
					i++;
					QuestionDTO resultItem = new QuestionDTO();
	
					//Set properties
					resultItem.setID(cursor.getString(cursor.getColumnIndex("ID")));
					//resultItem.setTypeID(cursor.getString(cursor.getColumnIndex("TypeID")));
					resultItem.setContent(cursor.getString(cursor.getColumnIndex("Content")));
		 			try{
		 				resultItem.setImagePath(cursor.getString(cursor.getColumnIndex("ImagePath")));
		 			}catch(Exception e){
		 				resultItem.setImagePath("");
		 			}
		 			
		 			ArrayList<AnswerDTO> arrAnswer = AnswerDTO.getAnswers(context, resultItem.getID());
		 			resultItem.setAnswers(arrAnswer);
		 			
		 			ArrayList<CorrectAnswerDTO> arrCorrectAnswer = CorrectAnswerDTO.getCorrectAnswers(context, resultItem.getID());
		 			resultItem.setCorrectAnswers(arrCorrectAnswer);
		 			
		 			//Property Answers
		 			//ArrayList<AnswerDTO> arrAnswer = AnswerDTO.getAnswers(context, resultItem.getID());
		 			resultItem.setAnswers(AnswerDTO.getAnswers(context, resultItem.getID()));
		 			
					arrResults.add(resultItem, i);
				}while(cursor.moveToNext());
			}
		 	myDbHelper.close();
		 	return arrResults;		
		}
	
	//Get questions by key word
	/*public static ArrayList<QuestionDTO> GetQuestionByKeyWord(Context context, String keywords)
	{
		String query = "";
		if(keywords == null || keywords.isEmpty())
		{
			query = "SELECT * FROM Questions";
		}
		else
		{
			query = "SELECT * FROM Questions WHERE Content like '%"+keywords+"%'";
		}
		
		ArrayList<QuestionDTO> arrResults = new ArrayList<QuestionDTO>();
		DatabaseHelper myDbHelper = DatabaseHelper.getInstance(context);
		
	 	try {
	 		myDbHelper.openDatabase();
	 	}catch(SQLException sqle){
	 		throw sqle;
	 	}
	 	
	 	Cursor cursor = myDbHelper.getMyDataBase().rawQuery(query, null);
		if(cursor.moveToFirst()){
			do{
				QuestionDTO resultItem = new QuestionDTO();

				//Set properties
				resultItem.setID(cursor.getString(cursor.getColumnIndex("ID")));
				//resultItem.setTypeID(cursor.getString(cursor.getColumnIndex("TypeID")));
				resultItem.setContent(cursor.getString(cursor.getColumnIndex("Content")));
	 			try{
	 				resultItem.setImagePath(cursor.getString(cursor.getColumnIndex("ImagePath")));
	 			}catch(Exception e){
	 				resultItem.setImagePath("");
	 			}
	 			
	 			//Property Answers
	 			//ArrayList<AnswerDTO> arrAnswer = AnswerDTO.getAnswers(context, resultItem.getID());
	 			resultItem.setAnswers(AnswerDTO.getAnswers(context, resultItem.getID()));
	 			
				arrResults.add(resultItem);
			}while(cursor.moveToNext());
		}
	 	myDbHelper.close();
	 	return arrResults;
	}*/
	
	public static ArrayList<QuestionDTO> RandomQuestionsByLicenseType(Context context, String licenseType)
	{
		ArrayList<Integer> questionNumberList =  RandomIntArrayQuestionsByLicenseType(context,licenseType);
		ArrayList<String> listQuestionIDs = new ArrayList<String>();
		for(int i = 0; i<questionNumberList.size(); i++)
		{
			listQuestionIDs.add(createID(questionNumberList.get(i)));
		}
		return GetQuestionByListID(context, listQuestionIDs);
	}
	public static ArrayList<Integer> RandomNumber(int numOfNumber, int min, int max)
	{
		ArrayList<Integer> result = new ArrayList<Integer>();
		Random rand = new Random();
		do{
			Integer  number = rand.nextInt(max - min) + min;
			if(!result.contains(number))
				result.add(number);
		}
		while(result.size()<numOfNumber);
		return result;
	}
	public static ArrayList<Integer> RandomIntArrayQuestionsByLicenseType(Context context, String licenseType)
	{
		ArrayList<Integer> result = new ArrayList<Integer>();
		//B2, C, D, E va cac hang F
		if(licenseType == "B2" || licenseType == "C" || licenseType == "D" || licenseType == "E" || licenseType == "F")
		{
			//9 cau ve khai niem va quy tac giao thong
			result.addAll(RandomNumber(9, 1, 145));
			//1 cau ve nghiep vu van tai
			result.addAll(RandomNumber(1, 146, 175));
			//1 cau ve van hoa va dao duc nguoi lai xe
			result.addAll(RandomNumber(1, 176, 200));
			//1 cau ve ky thuat lai xe va cau tao sua chua
			result.addAll(RandomNumber(1, 201, 255));
			//9 cau ve he thong bien bao
			result.addAll(RandomNumber(9, 256, 355));
			//9 cau ve giai cac the sa hinh
			result.addAll(RandomNumber(9, 356, 450));
		}
		if(licenseType == "B1")
		{
			//9 cau ve khai niem va quy tac giao thong
			result.addAll(RandomNumber(9, 1, 145));
			//2 cau ve van hoa va dao duc nguoi lai xe
			result.addAll(RandomNumber(2, 176, 200));
			//1 cau ve ky thuat lai xe va cau tao sua chua
			result.addAll(RandomNumber(1, 201, 255));
			//9 cau ve he thong bien bao
			result.addAll(RandomNumber(9, 256, 355));
			//9 cau ve giai cac the sa hinh
			result.addAll(RandomNumber(9, 356, 450));
		}
		if(licenseType == "A3" || licenseType == "A4")
		{
			ArrayList<Integer> arrTemp = new ArrayList<Integer>();
			//9 cau ve khai niem va quy tac giao thong
			arrTemp = RandomNumber(9, 1, 145);
			//Loai bo gia tri 119
			if(arrTemp.contains(119))
			{
				arrTemp.remove(119);
				int number = RandomNumber(1, 1, 145).get(0);
				do
				{
					if(!arrTemp.contains(number)&& number!=119)
					{
						arrTemp.add(RandomNumber(1, 1, 145).get(0));
						break;
					}
					else
						number = RandomNumber(1, 1, 145).get(0);
				}while(true);
			}
			//Loai bo gia tri 120
			if(arrTemp.contains(120))
			{
				arrTemp.remove(120);
				int number = RandomNumber(1, 1, 145).get(0);
				do
				{
					if(!arrTemp.contains(number)&& number!=120)
					{
						arrTemp.add(RandomNumber(1, 1, 145).get(0));
						break;
					}
					else
						number = RandomNumber(1, 1, 145).get(0);
				}while(true);
			}
			
			result.addAll(arrTemp);
			//1 cau ve nghiep vu van tai
			int number = RandomNumber(1, 146, 175).get(0);
			do
			{
				if(number!=147&&number!=152&&number!=166)
				{
					break;
				}
				else
					number = RandomNumber(1, 146, 175).get(0);
			}while(true);
			
			result.add(number);
			
			//1 cau ve van hoa va dao duc nguoi lai xe
			result.addAll(RandomNumber(1, 176, 200));
			//5 cau ve he thong bien bao
			result.addAll(RandomNumber(5, 256, 355));
			//4 cau ve giai cac the sa hinh
			result.addAll(RandomNumber(4, 356, 450));
		}
		if(licenseType == "A1" || licenseType == "A2")
		{
			ArrayList<Integer> arrTemp = new ArrayList<Integer>();
			//10 cau luat
			arrTemp.addAll(RandomNumber(10, 0, 79));
			//5 cau bien bao
			arrTemp.addAll(RandomNumber(5, 80, 114));
			//5 cau sa hinh
			arrTemp.addAll(RandomNumber(5, 115, 149));
			for(int i = 0; i<arrTemp.size(); i++)
			{
				result.add(ArrayA1A2[arrTemp.get(i)]);
			}
		}
		return result;
	}
}
